# 任务：预测房价

# 建立多因子模型进行预测
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# 获取数据
data = pd.read_csv('usa_housing_price.csv')
# Avg. Area Income,Avg. Area House Age,Avg. Area Number of Rooms,Area Population,size,Price
x_multi = data.drop(['Price'], axis=1)  # 排除价格

#进行训练
lr_multi_model = LinearRegression()
lr_multi_model.fit(x_multi, data['Price'])
y_predict_multi = lr_multi_model.predict(x_multi)

# 评估模型
mse = mean_squared_error(data['Price'], y_predict_multi)
r2 = r2_score(data['Price'], y_predict_multi)
print('均方差:', mse)
print('R2分数:', r2)

# 进行预测
x_test = [65000, 5, 5, 30000, 200]
x_test = np.array(x_test).reshape(1, -1)
y_test_predict = lr_multi_model.predict(x_test)
print(y_test_predict)